
//PHASE 3: DRAG AND DROP
	
			function mouseMoveHandler(event:MouseEvent):void 
            {                
                var dragInitiator:Image = Image(event.currentTarget);
                
                var ds:DragSource = new DragSource();
                ds.addData(dragInitiator, "img");               

                DragManager.doDrag(dragInitiator, ds, event);
                
            }
            function mouseMoveHandler2(event:MouseEvent):void 
            {                
                var dragInitiator:Image = Image(event.currentTarget);
                var ds:DragSource = new DragSource();
                ds.addData(dragInitiator, "EditImg");               
                DragManager.doDrag(dragInitiator, ds, event);
				
				if(Canvas2ImSort.length*120>Canvas3.width)
				{
					var xPos:int = event.stageX - Canvas3.x;				
					var box:int = Canvas3.width/Canvas2ImSort.length;
					var target:int = Math.floor(xPos/box);
	
					var xx:int=120*(target+0.5)+target-xPos;
					if(xx>0)
						Canvas2.x=Canvas2.x-2;
					else if(xx<0)
						Canvas2.x=Canvas2.x+2;
       
				}
            }	
			function dragOverHandler(event:DragEvent):void
            {
                if (event.dragSource.hasFormat("img") || event.dragSource.hasFormat("EditImg")) {
                    if (event.ctrlKey) {                    
                        DragManager.showFeedback(DragManager.COPY);
                        return;
                    }
                    else {
                        DragManager.showFeedback(DragManager.MOVE);
                        return;
                    }
                }
                DragManager.showFeedback(DragManager.NONE);
            }
        	
            function dragEnterHandler(event:DragEvent):void {
           		var ss:Canvas = Canvas(event.currentTarget);
           		if (event.dragInitiator.parent == Canvas1)
           		{	
           			DragManager.acceptDragDrop(Canvas2);
           		}
           		if (event.dragInitiator.parent == Canvas2)
        		{
        			DragManager.acceptDragDrop(Canvas2);
        		}
            }
            
            
        	function dragDropHandler(event:DragEvent):void {
				if (event.dragSource.hasFormat("img")) {
                  var draggedImage:Image = event.dragSource.dataForFormat('img') as Image;
                  var dropCanvas:Canvas = event.currentTarget as Canvas;
              
                  // Since this is a copy, create a new object to 
                  // add to the drop target.
                  var num:int = dropCanvas.getChildren().length;
                  var newImage:Image=new Image();
                  newImage.source = draggedImage.source;
                  newImage.x = num*(Canvas2ImWid+1);
                  newImage.y = 0;
                  newImage.maintainAspectRatio = false;
                  newImage.width = Canvas2ImWid;//dropCanvas.mouseY;
                  newImage.height = Canvas2ImWid/4*3; 
				  newImage.addEventListener(MouseEvent.CLICK, Canvas2Thumb_click);
				  newImage.addEventListener(DragEvent.DRAG_COMPLETE, dragCompleteHandler2);
				  newImage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler2);
				  
                  dropCanvas.addChild(newImage);
                  
                  Canvas2ImSort[Canvas2ImSort.length] = newImage;
                  if(Canvas2ImSort.length>6)
	                  dropCanvas.width = Canvas2ImSort.length*(120+1); 
                  
                  
        		}
        		if (event.dragSource.hasFormat("EditImg")) {
        			var draggedImage:Image = event.dragSource.dataForFormat('EditImg') as Image;
                  	var dropCanvas:Canvas = event.currentTarget as Canvas;
                  	
                  	var Pos:int = Math.floor(event.localX/120);
					var temp:Array = new Array();
					var original:int = Canvas2ImSort.indexOf( draggedImage);
					
					if (Pos <= Canvas2ImSort.length)
					{		
						if(Pos>original)				
						{
							for( var i=0; i<original; i++)
							{	
								Canvas2ImSort[i].x = (120+1)*i;
								temp[i]=Canvas2ImSort[i];	
							}
							for(var i=original; i<Pos; i++)
							{	
								Canvas2ImSort[i+1].x = (120+1)*i;
								temp[i]=Canvas2ImSort[i+1];	
							}
							draggedImage.x = Pos*(120+1);
							temp[Pos]=draggedImage;
							for(var i=Pos+1; i<Canvas2ImSort.length; i++)
							{	
								Canvas2ImSort[i].x = (120+1)*i;
								temp[i]=Canvas2ImSort[i];		
							}
							Canvas2ImSort = temp;
						}
						else if(Pos<original)
						{
							for(var i=0; i<Pos; i++)
							{
								Canvas2ImSort[i].x = (120+1)*i;
								temp[i]=Canvas2ImSort[i];	
							}
							draggedImage.x = Pos*(120+1);
							temp[Pos]=draggedImage;
							for(var i=Pos+1; i<=original ;i++)
							{
								Canvas2ImSort[i-1].x = (120+1)*i;
								temp[i] = Canvas2ImSort[i-1];
							}
							for(var i=original+1; i<Canvas2ImSort.length; i++)
							{
								Canvas2ImSort[i].x = (120+1)*i;
								temp[i]=Canvas2ImSort[i];
							}
							Canvas2ImSort = temp;
							
						}					
					}
        		}
        	}
        	function dragCompleteHandler(event:DragEvent):void {
                var draggedImage:Image = event.dragInitiator as Image;
                var dragInitCanvas:Canvas = event.dragInitiator.parent as Canvas;

         //       if (event.action == DragManager.MOVE)
         ///           dragInitCanvas.removeChild(draggedImage);
            } 
            function dragCompleteHandler2(event:DragEvent):void {
            	var draggedImage:Image = event.dragInitiator as Image;
                var dragInitCanvas:Canvas = event.dragInitiator.parent as Canvas;

   //             if (event.action == DragManager.MOVE)
   //             	dragInitCanvas.removeChild(draggedImage);
            }// ActionScript file
